package com.example.caimogu.fragment;


import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.caimogu.bean.Goods;
import com.example.caimogu.businessapp.AddNewGoodsActivity;
import com.example.caimogu.businessapp.MainActivity;
import com.example.caimogu.businessapp.R;
import com.example.caimogu.fragmentAdapter.MyStoreAdapter;
import com.example.caimogu.fragmentAdapter.UnhandledOrderAdapter;
import com.example.caimogu.fragmentImpl.MyStoreImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *我的店
 * 对店里的goods,添加，修改，上架
 * Created by Caimogu on 2016/4/20.
 */
public class MyStore extends Fragment {
    private Context context;
    private Button btn_addNewGoods;
    private Button btn_commitGoods;
    private ListView listView;
    private MyStoreAdapter myStoreAdapter;
    private LayoutInflater layoutInflater;
    private List<Goods> gl;
    private  List<String> fileList;
    private List<String> fileListParent;
    private int picNum = 0;

    public MyStore(Context context, LayoutInflater layoutInflater){
        this.context = context;
        this.layoutInflater = layoutInflater;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mystore,container,false);
       gl = new ArrayList<Goods>();
        fileListParent = new ArrayList<String>();
        //initData();
        init(view);

        return view;
    }

    private void init(View view) {
        //myStoreAdapter = new MyStoreAdapter(goodsList,layoutInflater);
        listView = (ListView)view.findViewById(R.id.lv_ms_goodslist);
        //listView.setAdapter(myStoreAdapter);
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//            }
//        });
        btn_addNewGoods = (Button)view.findViewById(R.id.btn_ms_addNewGoods);
        btn_addNewGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addNewGoods();
            }
        });
        btn_commitGoods = (Button)view.findViewById(R.id.btn_ms_commitGoods2server);
        btn_commitGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commitGoods2Server();
            }
        });
    }

    public void addNewGoods(){
        Toast.makeText(context, "addNewGoods test ", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.setClass(context, AddNewGoodsActivity.class);
        startActivityForResult(intent, Activity.RESULT_FIRST_USER);

    }

    public void commitGoods2Server(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                MyStoreImpl myStoreImpl = new MyStoreImpl();
                myStoreImpl.addNewGoodsImpl(gl, fileListParent);
            }
        }).start();


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("caimogu","onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Activity.RESULT_FIRST_USER) {
            if (resultCode == Activity.RESULT_OK) {
                Log.i("caimogu", "onActivityResult");
                Bundle b = data.getExtras();

                Goods goods = new Goods();
                //goods.setGid("zjm001");
                //很机智啊-------------((MainActivity)getActivity()).getOnlyBid()----------------
                String bid = ((MainActivity)getActivity()).getOnlyBid();
                goods.setBid(bid);
                //Log.i("caimogu",bid);
                //goods.setBid("2222");
                goods.setName(b.getString("goodsName"));
                goods.setType(1);
                goods.setPrice(Integer.parseInt(b.getString("goodsprice")));
                goods.setSelltime(b.getString("goodsSelltime"));
                goods.setStatus(1);
                goods.setInformation(b.getString("goodsInfo"));

                fileList = new ArrayList<String>();
                ArrayList<String> photoList = null;
                photoList = b.getStringArrayList("photoList");
                for(String s:photoList){
                    fileList.add(s);
                    fileListParent.add(s);
                }
                //fileList.add(b.getString("imageUri"));
                //fileList.add("/mnt/sdcard/Pictures/sheep.png");
                //fileList.add("/mnt/sdcard/Pictures/sheep.png");

                List<String> picList = new ArrayList<String>();
                int j = picNum;
                for(int i=picNum; i<fileList.size()+j;i++){
                    picList.add("pic"+picNum);
                    picNum++;
                }
                goods.setPicPath(picList);

                gl.add(goods);
                //fileListParent.add(fileList);
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);

            }

        }
    }
    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1){
                Log.i("caimogu","handleMessage..");
//                Bundle b = msg.getData();
//                List<Order> orders = (ArrayList)b.getSerializable("orders");
                if(gl != null) {
                    //myStoreAdapter = new MyStoreAdapter(gl, layoutInflater,context,fileList);
                    myStoreAdapter = new MyStoreAdapter(gl,fileListParent, layoutInflater,context,fileList);
                    listView.setAdapter(myStoreAdapter);
                }else{
                    Toast.makeText(context, " 没添加上商品 ", Toast.LENGTH_SHORT).show();
                }

            }
        }
    };



}
